﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BLL;

namespace GUI
{
    public partial class GioHang : System.Web.UI.Page
    {
        #region Khai báo
        protected List<GioHangDTO> gioHang;
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    gioHang = new List<GioHangDTO>();
                    if (Session["GioHang"] != null)
                    {
                        gioHang = (List<GioHangDTO>)Session["GioHang"];
                        loadGridView(gioHang);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        #endregion

        #region Load thông tin giỏ hàng
        public void loadGridView(List<GioHangDTO> lstGioHang)
        {
            GVGioHang.DataSource = gioHang;
            GVGioHang.DataBind();

            if (lstGioHang.Count == 0)
            {
                VisibleButton();
            }
        }

        private void VisibleButton()
        {
            btnCapNhat.Visible = false;
            btnMuaHang.Visible = false;
            btnTiepTucMuaHang.Visible = false;
            btnXoaGioHang.Visible = false;
        }
        #endregion

        #region LoadTongTien
        protected string LoadTongTien(object ob)
        {
            decimal sum = 0;
            gioHang = (List<GioHangDTO>)Session["GioHang"];

            int _n = gioHang.Count;
            for (int i = 0; i < _n; i++)
            {
                sum += gioHang[i].GiaBan * gioHang[i].SoLuong;
            }
            return string.Format("{0:0,0 VNĐ}", sum);
        }
        #endregion

        #region LoadThanhTien
        protected string LoadThanhTien(object giaban, object soluong)
        {
            decimal sum = 0;
            int sl = (int)soluong;
            decimal gb = (decimal)giaban;
            sum = sl * gb;
            return string.Format("{0:0,0 VNĐ}", sum);
        }
        #endregion

        #region Cập nhật giỏ hàng
        public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
        #endregion

        #region Cập nhật số lượng
        public void capNhatSoLuongSanPham(int i, string masp, int soluong)
        {
            if (gioHang[i].MaSanPham == masp)
            {
                gioHang[i].SoLuong = soluong;
                Session["GioHang"] = gioHang;
            }
        }
        #endregion

        #region btnTiepTucMuaHang_Click
        protected void btnTiepTucMuaHang_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
        #endregion

        #region btnXoaGioHang_Click
        protected void btnXoaGioHang_Click(object sender, EventArgs e)
        {
            //Session.Contents.Remove("GioHang");
            //Response.Redirect("GioHang.aspx");

            this.gioHang = new List<GioHangDTO>();
            Session["GioHang"] = null;

            //this.loadGridView(gioHang);
            Response.Redirect("Default.aspx");
        }
        #endregion

        #region btnCapNhat_Click
        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            int _n = GVGioHang.Rows.Count;
            for (int i = 0; i < _n; i++)
            {
                GridViewRow gvrow = GVGioHang.Rows[i];
                TextBox txtsoluong = (TextBox)gvrow.Cells[2].FindControl("txtsoluong");
                if (IsNumber(txtsoluong.Text) == true)
                {
                    int soluong = int.Parse(txtsoluong.Text);
                    if (soluong > 100)
                    {
                        soluong = 99;
                    }
                    string masp = ((Label)gvrow.Cells[1].FindControl("lblmasanpham")).Text;
                    gioHang = (List<GioHangDTO>)Session["GioHang"];
                    capNhatSoLuongSanPham(i, masp, soluong);

                }
            }
            loadGridView(gioHang);
        }
        #endregion

        #region GVGioHang_RowDataBound
        protected void GVGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#bde2fd'");

                if (e.Row.RowIndex % 2 == 0)
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='#EFF3FB'");
                else
                    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor='White'");
            }
        }
        #endregion

        #region GVGioHang_RowCommand
        protected void GVGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("xoa"))
                {
                    if (Session["GioHang"] != null)
                    {
                        gioHang = new List<GioHangDTO>();
                        gioHang = (List<GioHangDTO>)Session["GioHang"];
                        string masp = (string)e.CommandArgument;
                        int _n = gioHang.Count;
                        for (int i = 0; i < _n; i++)
                        {
                            GioHangDTO spGioHang = gioHang[i];
                            if (spGioHang.MaSanPham == masp) // san pham co trong gio hang
                            {
                                gioHang.RemoveAt(i);
                                Session["GioHang"] = gioHang;
                                break;
                            }
                        }
                    }
                    this.loadGridView(gioHang);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            //Response.Redirect("GioHang.aspx");
        }
        #endregion

        #region btnMuaHang_Click
        protected void btnMuaHang_Click(object sender, EventArgs e)
        {
            this.gioHang = new List<GioHangDTO>();
            this.gioHang = (List<GioHangDTO>)Session["GioHang"];

            if (Session["KhachHang"] == null)
            {
                this.LabelThongBaoDangNhap.Visible = true;
                return;
            }

            System.Threading.Thread.Sleep(5000);

            Response.Redirect("ThanhToan.aspx");
        }
        #endregion

    }
}